# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=bullet
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=2.83
pkgrel=1
pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and  animation (mingw-w64)"
arch=('any')
url="http://www.bulletphysics.com/Bullet/"
license=('custom:zlib')
options=('!strip' 'staticlibs')
#optdepends=('bullet-docs: documentation')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-freeglut")
source=(#"http://bullet.googlecode.com/files/bullet-${pkgver}-${_rev}.tgz"
        https://github.com/bulletphysics/bullet3/archive/Bullet-${pkgver}-alpha.tar.gz
        remove-absent-demo.patch
        install-pkgconfig-files-mingw.patch
        cast-error.patch)
md5sums=('f459bb08387eb5696467109f44c1d4fd'
         '70eda7f29f87cc3b65b58cb417e14201'
         '2c243be67782ceb32a96aad61e668e83'
         '8db1300cf01a0fb244cb4b3ea5eb102e')

prepare () {
  cd ${srcdir}/bullet3-Bullet-${pkgver}-alpha
  patch -p1 -i ${srcdir}/remove-absent-demo.patch
  patch -p1 -i ${srcdir}/install-pkgconfig-files-mingw.patch
  patch -p1 -i ${srcdir}/cast-error.patch
}

build() {
  plain "No step"
}

build_release() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-freeglut")
  
  cd ${srcdir}
  mkdir -p build-release && pushd build-release
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX}  \
    -DBUILD_SHARED_LIBS=1 \
    -DBUILD_EXTRAS=1 \
    -DINSTALL_LIBS=1 \
    -DUSE_GLUT=1 \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=1 \
    -DCMAKE_BUILD_TYPE=Release \
    ../bullet3-Bullet-${pkgver}-alpha
  make
  make install
  
  mv -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/bullet.pc
}

build_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver")
  
  cd ${srcdir}
  mkdir -p build-debug && pushd build-debug
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX}  \
    -DBUILD_SHARED_LIBS=1 \
    -DBUILD_EXTRAS=1 \
    -DINSTALL_LIBS=1 \
    -DUSE_GLUT=1 \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=1 \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_DEBUG_POSTFIX="_d" \
    ../bullet3-Bullet-${pkgver}-alpha
  make
  make install
  
  mv -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  
  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig

}

package_mingw-w64-i686-bullet() {
  build_release
}

package_mingw-w64-i686-bullet-debug() {
  build_debug
}

package_mingw-w64-x86_64-bullet() {
  build_release
}

package_mingw-w64-x86_64-bullet-debug() {
  build_debug
}

package() {
  cd "${srcdir}/bullet-${pkgver}-${_rev}/build-${MINGW_CHOST}"
  make install  # DESTDIR="${pkgdir}${MINGW_PREFIX}" 
  install -d "${pkgdir}${MINGW_PREFIX}"/bin
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/bullet.pc
}
